Telnet Negotiation Examples

您所在的位置:网站首页 telnet server-source -i配置不了 Telnet Negotiation Examples

Telnet Negotiation Examples

2024-06-28 02:23| 来源: 网络整理| 查看: 265

See all PowerTCP Telnet for .NET Code Examples

聽Open Negotiation Example

The following examples demonstrates manual client/server option negotiation. Typically, the Telnet component will handle option negotiation automatically. However, the developer also has the flexibility to manually set options to be used and rejected.

//Standard telnet port, add standard telnet options.telnet1.ClientOptions.Add(new聽Option(OptionCode.SuppressGoAheads,聽聽聽聽聽聽聽聽聽OptionState.RequestOn));telnet1.ClientOptions.Add(new聽Option(OptionCode.WindowSize,聽null,聽聽聽聽聽聽聽 new聽Byte[] { ((Byte)(0)), ((Byte)(80)), ((Byte)(0)), ((Byte)(24)) },聽聽聽聽聽聽聽聽聽OptionState.RequestOn));telnet1.ClientOptions.Add(new聽Option(OptionCode.TerminalType,聽聽聽聽聽聽聽 new聽Byte[] { ((Byte)(0)), ((Byte)(116)), ((Byte)(116)), ((Byte)(121)) },聽聽聽聽聽聽聽聽聽OptionState.RequestOn));

telnet1.ServerOptions.Add(new聽Option(OptionCode.SuppressGoAheads,聽null,聽聽聽聽聽聽聽聽OptionState.RequestOn));telnet1.ServerOptions.Add(new聽Option(OptionCode.Echo,聽null,聽OptionState.RequestOn));telnet1.ServerOptions.Add(new聽Option(OptionCode.OutputPageSize,聽null,聽聽聽聽聽聽聽聽OptionState.RequestOn));

In addition, negotiation can be handled in real-time, using the CommandReceived event and SendXXX commands. In the following example, explicit security is聽negotiated in the CommandReceived event handler.

using System.Net.Security; using System.Security.Cryptography.X509Certificates;

Telnet telnet1 = new Telnet();

private聽void聽telnet1_CommandReceived(object聽sender,聽CommandEventArgs聽e){聽聽聽 //Handles negotiation for an explicitly secure connection.聽聽聽 try聽聽聽 {聽聽聽聽聽聽聽 if聽(e.OptionCode ==聽OptionCode.Authentication)聽聽聽聽聽聽聽 {聽聽聽聽聽聽聽聽聽聽聽 if聽(e.Command ==聽Command.Do)聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 telnet1.SendOption(Command.Will,聽OptionCode.Authentication);聽聽聽聽聽聽聽聽聽聽聽 else聽if聽(e.Command ==聽Command.SB && e.SubOption[0] == 1)聽聽聽聽聽聽聽聽聽聽聽 {聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 //Server expects a SEND request (IS).聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 byte[] response =聽new聽byte[4];聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽response[0] = 0;聽// is聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 response[1] = 7;聽// ssl聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 response[2] = 0;聽// AuthClientToServer聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 response[3] = 1;聽// START_SSL is our request聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 telnet1.SendSubOption(e.OptionCode, response);聽聽聽聽聽聽聽聽聽聽聽 }聽聽聽聽聽聽聽聽聽聽聽 else聽if聽(e.Command ==聽Command.SB && e.SubOption[0] == 2)聽聽聽聽聽聽聽聽聽聽聽 {聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 //Setup client security with the target host, and a callback that聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 //executes when the remote host presents its certificate.聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 ClientSecurity聽security =聽new聽ClientSecurity();聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 security.TargetHost =聽"mySecureHost";聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 security.ValidationCallback += remoteCertificateValidation;

聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 //Authenticate the聽server.聽聽聽聽聽聽聽聽聽聽聽聽聽聽聽 telnet1.AuthenticateAsClient(security);聽聽聽聽聽聽聽聽聽聽聽 }聽聽聽聽聽聽聽 }聽聽聽 }聽聽聽 catch聽(Exception聽ex)聽聽聽 {聽聽聽聽聽聽聽聽//Close the connection聽if there is an error.聽聽聽聽聽聽聽聽telnet1.Close();聽聽聽 }}

private bool remoteCertificateValidation(聽 聽object sender,聽 聽X509Certificate remoteCertificate,聽 聽X509Chain chain,聽 聽SslPolicyErrors sslPolicyErrors){聽 聽 return true; //Accepts any certificate, even if invalid}

To download a trial please visit the聽PowerTCP Telnet for .NET聽product page.



【本文地址】


今日新闻


推荐新闻


CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3